package com.zhuangjie.shop.controller;


import com.zhaungjie.base.conduit.R;
import com.zhuangjie.security.annotations.NoAuthentication;
import com.zhuangjie.security.entity.AuthenticationTokens;
import com.zhuangjie.security.helper.JwtHelper;
import com.zhuangjie.security.intercepor.UserRequestInterceptor;
import com.zhuangjie.shop.dto.LoginDTO;
import com.zhuangjie.shop.entity.Menu;
import com.zhuangjie.shop.service.MenuService;
import com.zhuangjie.shop.service.MerchantAccountService;
import com.zhuangjie.shop.vo.LoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * <p>
 * 商家账号 前端控制器
 * </p>
 *
 * @author zhuangjie
 * @since 2023-09-24
 */
@RestController
@RequestMapping("/merchant-account")
@Api(tags="店铺账号")
public class MerchantAccountController {
    @Autowired
    private MerchantAccountService merchantAccountService;

    @Autowired
    private MenuService menuService;

    @Autowired
    private UserRequestInterceptor userRequestInterceptor;


    @Autowired
    private JwtHelper jwtHelper;

    @PostMapping("/login")
    @ApiOperation("商家登录")
    @NoAuthentication
    public R login(@Valid @RequestBody LoginDTO loginDTO) throws Exception {
        AuthenticationTokens authenticationTokens = merchantAccountService.login(loginDTO);
        // 直接挂载登录状态，下面获取Menu列表需要
        userRequestInterceptor.tryNotCheckStateMountAnyModuleLoginState(authenticationTokens.access_token,false);
        // 获取Menu列表
        List<Menu> menuList = menuService.queryList();
        LoginVO loginVO = LoginVO.builder().menuList(menuList).authenticationTokens(authenticationTokens).build();
        return R.ok().data(loginVO);
    }
    @PostMapping("/refreshToken")
    @ApiOperation("刷新令牌")
    @NoAuthentication
    public R refreshToken(@NotBlank @RequestParam("refreshToken") String refreshToken) throws Exception {
        // 登录逻辑
        return R.ok().data(merchantAccountService.refreshToken(refreshToken));
    }


}

